package com.um.jdy.app.shop.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaIgnore;
import com.um.jdy.app.shop.entity.bo.AddressBO;
import com.um.jdy.app.shop.entity.bo.GoodsBO;
import com.um.jdy.app.shop.entity.bo.OrderBO;
import com.um.jdy.app.shop.entity.dto.*;
import com.um.jdy.app.shop.entity.vo.OrderDetailVO;
import com.um.jdy.app.shop.entity.vo.ShopOrderItemVO;
import com.um.jdy.app.shop.factory.OrderFactory;
import com.um.jdy.app.shop.manager.ShopOrderManager;
import com.um.jdy.common.shop.enums.OrderEnum;
import com.um.springboot.starter.annotation.ActionLogRequired;
import com.um.springboot.starter.entity.Page;
import com.um.springboot.starter.orm.entity.Response;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * @author renjunwei
 * @date 2020/9/19 09:37
 */
@RestController
@RequestMapping("/api/v1/shop/order/")
@SaCheckLogin
@Validated
@Api(tags = "商城订单")
public class ShopOrderController {

    @Autowired
    ShopOrderManager shopOrderManager;

    @Autowired
    OrderFactory orderFactory;

    /**
     * 确认订单
     * @param dto
     * @return
     */
    @ApiOperation("确认订单")
    @PostMapping("confirm")
    public Response<OrderBO> confirm(@Valid @RequestBody ConfirmOrderDTO dto){
        List<GoodsBO> goodsBOList = orderFactory.initGoodsList(dto.getGoods());
        AddressBO addressBO = orderFactory.getAddress(dto.getMemberAddressId());

        return Response.success(orderFactory.confirmOrder(goodsBOList, addressBO,dto.getActivityCouponsId(),dto.getActivityCouponsGiftId(), OrderEnum.Type.Normal));
    }

    /**
     * 创建订单
     * @param dto
     * @return
     */
    @ApiOperation("创建订单")
    @PostMapping("create")
    public Response create(@Valid @RequestBody CreateOrderDTO dto){
        List<GoodsBO> goodsBOList = orderFactory.initGoodsList(dto.getGoods());
        AddressBO addressBO = orderFactory.getAddress(dto.getMemberAddressId());

        return Response.success(orderFactory.createOrder(goodsBOList, addressBO,dto.getActivityCouponsId(),
                dto.getActivityCouponsGiftId(), dto.getRemark(), dto.getPayWay()));
    }

    /**
     * 确认拼团订单
     * @param dto
     * @return
     */
    @ApiOperation("确认拼团订单")
    @PostMapping("confirm/group")
    public Response<OrderBO> confirmGroup(@Valid @RequestBody ConfirmGroupOrderDTO dto){
        List<GoodsBO> goodsBOList = orderFactory.initGoodsList(dto.getGoods());
        AddressBO addressBO = orderFactory.getAddress(dto.getMemberAddressId());

        return Response.success(orderFactory.confirmGroupOrder(goodsBOList, addressBO,dto.getActivityCouponsId(),dto.getActivityCouponsGiftId(), dto.getMarketGroupId()));
    }

    /**
     * 创建订单
     * @param dto
     * @return
     */
    @ApiOperation("创建拼团订单")
    @PostMapping("create/group")
    public Response createGroup(@Valid @RequestBody CreateGroupOrderDTO dto){
        List<GoodsBO> goodsBOList = orderFactory.initGoodsList(dto.getGoods());
        AddressBO addressBO = orderFactory.getAddress(dto.getMemberAddressId());

        return Response.success(orderFactory.createGroupOrder(goodsBOList, addressBO,dto.getActivityCouponsId(),
                dto.getActivityCouponsGiftId(), dto.getMarketGroupId(), dto.getRemark(), dto.getPayWay()));
    }

    /**
     * 分页查询我的订单
     * @param dto
     * @return
     */
    @ApiOperation("分页查询我的订单")
    @GetMapping("page")
    public Response<Page<ShopOrderItemVO>> getMyCommentPage(@Valid @ModelAttribute ShopOrderPageDTO dto){
        return Response.success(shopOrderManager.getPage(dto));
    }

    /**
     * 查询订单详情
     * @param id
     * @return
     */
    @ApiOperation("查询订单详情")
    @GetMapping("{id}")
    public Response<OrderDetailVO> get(@ApiParam("订单ID") @PathVariable String id){
        return Response.success(shopOrderManager.get(id));
    }

    /**
     * 修改收货地址
     * @param id
     * @return
     */
    @ApiOperation("修改收货地址")
    @GetMapping("update_address/{id}")
    public Response<OrderDetailVO> updateAddress(@ApiParam("订单ID") @PathVariable String id, @RequestBody UpdateAddressDTO dto){
        return Response.success(shopOrderManager.get(id));
    }

    /**
     * 支付订单
     * @param id
     * @return
     */
    @ApiOperation("支付订单")
    @PostMapping("pay/{id}")
    public Response<Map> cancel(@ApiParam("订单ID") @PathVariable String id, @RequestBody PayOrderDTO dto){
        return Response.success(shopOrderManager.pay(id, dto.getPayWay()));
    }

    /**
     * 取消订单
     * @param id
     * @return
     */
    @ApiOperation("取消订单")
    @PostMapping("cancel/{id}")
    public Response cancel(@ApiParam("订单ID") @PathVariable String id){
        shopOrderManager.cancel(id);
        return Response.success();
    }

    /**
     * 删除订单
     * @param id
     * @return
     */
    @ApiOperation("删除订单")
    @PostMapping("delete/{id}")
    public Response delete(@ApiParam("订单ID") @PathVariable String id){
        shopOrderManager.delete(id);
        return Response.success();
    }

    /**
     * 确认收货
     * @param id
     * @return
     */
    @ApiOperation("确认收货")
    @PostMapping("receive/{id}")
    public Response receive(@ApiParam("订单ID") @PathVariable String id){
        shopOrderManager.receive(id);
        return Response.success();
    }

    /**
     * 查询快递轨迹
     * @param id
     * @return
     */
    @ApiOperation("查询快递轨迹")
    @GetMapping("track/{id}")
    public Response getExpressTrack(@ApiParam("订单ID") @PathVariable String id){
        return Response.success(shopOrderManager.getExpressTrack(id));
    }

    /**
     * 押金微信支付成功回调
     * @param request
     * @param com_id
     * @return
     */
    @SaIgnore
    @ActionLogRequired
    @RequestMapping("wxpay/notify/{com_id}")
    public Response depositWxPayNotify(HttpServletRequest request, @PathVariable String com_id) {
        shopOrderManager.wxPayNotify(request, com_id);
        return Response.success();
    }


}
